package com.kang.controller;

import com.kang.entity.Label;
import com.kang.service.LabelService;
import com.kang.util.WebResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "标签接口")
@RestController
@RequestMapping("/admin/label")
@Slf4j
public class LabelController {

    @Autowired
    private LabelService labelService;

    @ApiOperation("标签列表")
    @GetMapping("findNodes")
    public WebResponse findNodes() {
        try {
            return WebResponse.resSuccess("标签列表查询成功",labelService.findNodes());
        } catch (Exception ex) {
            log.info("标签列表失败",ex);
            return WebResponse.resFail("标签列表失败",null);
        }
    }

    @ApiOperation("根据id查询标签")
    @GetMapping("findLabelById/{id}")
    public WebResponse findLabelById(@PathVariable("id") Integer id) {
        try {
            return WebResponse.resSuccess("根据id查询标签成功",labelService.findLabelById(id));
        } catch (Exception ex) {
            log.info("根据id查询标签失败",ex);
            return WebResponse.resFail("根据id查询标签失败",null);
        }
    }

    @ApiOperation("新增标签")
    @PostMapping("insertLabel")
    public WebResponse insertLabel(@RequestBody Label label, HttpServletRequest request) {
        try {
            return WebResponse.resSuccess("新增标签成功",labelService.insertLabel(label,request));
        } catch (Exception ex) {
            log.info("新增标签失败",ex);
            return WebResponse.resFail("新增标签失败",null);
        }
    }

    @ApiOperation("删除标签")
    @DeleteMapping("deleteLabel/{id}")
    public WebResponse deleteLabel(@PathVariable("id") Integer id) {
        try {
            return WebResponse.resSuccess("删除标签成功",labelService.deleteLabel(id));
        } catch (Exception ex) {
            log.info("删除标签失败",ex);
            return WebResponse.resFail("删除标签失败",null);
        }
    }

    @ApiOperation("更新标签")
    @PostMapping("updateLabel")
    public WebResponse updateLabel(@RequestBody Label label, HttpServletRequest request) {
        try {
            return WebResponse.resSuccess("删除标签成功",labelService.updateLabel(label,request));
        } catch (Exception ex) {
            log.info("更新标签失败",ex);
            return WebResponse.resFail("更新标签失败",null);
        }
    }

    /**
     * 2 车型标签
     * 6文章标签
     */
    @ApiOperation("获取所有标签")
    @GetMapping("getLabelByType")
    public WebResponse getLabelByType(Integer type) {
        try {
            return WebResponse.resSuccess("获取所有标签成功",labelService.getLabelByType(type));
        } catch (Exception ex) {
            log.info("获取所有标签失败",ex);
            return WebResponse.resFail("获取所有标签失败",null);
        }
    }

}
